<?php

namespace app\api\controller;

class Classlist
{
    public $pagesize = 10;

    /**
     * 获挑战取列表信息
     */
    public function listsHot()
    {
        //
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数
        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array('hot' => 1); //--推荐
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架

        $classlistModel = new \app\admin\model\Classlist();
        $classlist = $classlistModel->lists($w, $this->pagesize);
        $lists = addIsJoinClasslistStatus($classlist['data'], $userInfo['id']);

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }

    public function isAddClassList()
    {

        $auth = trim(input("request.auth"));//用户auth
        $class_id = trim(input("request.class_id"));//用户auth
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($class_id)) {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        //get t_classlist message for id
        $classlistInfo = getClasslistInfo($class_id);
        if (empty($classlistInfo)) {
            $return = ['err' => 3, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        $flag = 0;
        if (isJoinInClass($userInfo['id'], $classlistInfo['id'])) {
            $flag = 1;
        }
        $return = ['err' => 0, 'msg' => '获取成功', 'devMsg' => '获取成功', "data" => $flag];
        return json($return);
    }

    /**
     * 用户挑战报名接口
     */
    public function baoming()
    {
        $auth = trim(input("request.auth"));//用户auth
        $class_id = trim(input("request.class_id"));//用户auth
        $num = trim(input("request.num"));//购买数量
        $userInfo = checkLoginExp($auth);

        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($num)) {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[num]'];
            return json($return);
        }
        if (empty($class_id)) {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        //get t_classlist message for id
        $classlistInfo = getClasslistInfo($class_id);
        if (empty($classlistInfo)) {
            $return = ['err' => 3, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        if ($classlistInfo['stats'] == 1 || $classlistInfo['is_delete'] == 1) {
            $return = ['err' => 4, 'msg' => '挑战已经下架', 'devMsg' => '挑战已经下架'];
            return json($return);
        }
        if ($classlistInfo['money'] > 0) {
            $return = ['err' => 5, 'msg' => '挑战需要支付，请先支付', 'devMsg' => '挑战需要支付，请先支付'];
            return json($return);
        }
        if ($num > $classlistInfo['max']) {
            $return['err'] = 11001;
            $return['msg'] = '购买数量已经超过最大值';
            return json($return);
        }

        if (isJoinInClass($userInfo['id'], $classlistInfo['id'])) {
            $return = ['err' => 6, 'msg' => '对不起，您已经加入了该挑战', 'devMsg' => '对不起，您已经加入了该挑战'];
            return json($return);
        }
        $data = array(
            'uid' => $userInfo['id'],
            'classid' => $classlistInfo['id'],
            'payno' => "",
            'ctime' => date('Y-m-d H:i:s'),
            'stat' => 0,
            'money' => 0,
            'price' => 0,
            'num' => $num,
        );
        $result = joinInClass($data);
        if ($result) {
            addPlanCheckIn($userInfo['id'], $classlistInfo);
            $return = ['err' => 0, 'msg' => '操作成功', 'devMsg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 6, 'msg' => '操作失败', 'devMsg' => '操作失败'];
            return json($return);
        }
    }


    /**
     * 挑战详情接口
     */
    public function info()
    {
        $auth = trim(input("request.auth"));//用户auth
        $class_id = trim(input("request.class_id"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        if (empty($class_id)) {
            $return = ['err' => 2, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        //get t_classlist message for id
        $classlistInfo = getClasslistInfo($class_id);
        if (empty($classlistInfo)) {
            $return = ['err' => 3, 'msg' => '参数错误', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }
        if ($classlistInfo['stats'] == 1 || $classlistInfo['is_delete'] == 1) {
            $return = ['err' => 4, 'msg' => '挑战已经下架', 'devMsg' => '挑战已经下架'];
            return json($return);
        }
        $classlistInfo['joinStats'] = 0;
        if (isJoinInClass($userInfo['id'], $classlistInfo['id'])) {
            $classlistInfo['joinStats'] = 1;
        }

        $return = ['err' => 0, 'msg' => '操作成功', 'devMsg' => '操作成功', 'data' => $classlistInfo];
        return json($return);
    }


    /**
     * 获取挑战列表数据接口
     */
    public function lists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数
        $hot = trim(input("hot"));//每页条数
        if (empty($hot)) {
            $hot = 0;
        } else {
            $hot = 1;
        }
        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架
        if($hot)
        {
            $w['hot']=$hot;
        }

        $classlistModel = new \app\admin\model\Classlist();
        $classlist = $classlistModel->lists($w, $this->pagesize, "*", "id desc");
        $lists = addIsJoinClasslistStatus($classlist['data'], $userInfo['id']);
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);

    }


    /**
     * 获取我加入的挑战
     */
    public function myLists()
    {
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }
        $pagesize = trim(input("pagesize"));//每页条数
        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $page = trim(input("page"));//每页条数
        if (empty($page)) {
            $page = 1;
        }
        $w = array('id' => $userInfo['id']);
        $classlistModel = new \app\admin\model\ClassUserjoin();

        $sql = "select a.*,
        b.payno as myclass_payno,
        b.ctime as myclass_join_time,
        b.stat as myclass_stat,
        b.money as myclass_money,
        b.num as myclass_num,
        b.price as myclass_price,
        b.js_stats as js_stats,
        b.is_win as is_win,
        b.reward_money as reward_money,
        b.win_money as win_money
        from  t_classlist as a,t_class_userjoin as b
        where a.id = b.classid and b.uid = {$userInfo['id']} order by b.id desc ";

        $classlist = $classlistModel->listsForSQL($sql, $this->pagesize, $page);
        $classlist = $classlist['data'];

        //判断今日是否已经打卡
        if ($classlist) {
            foreach ($classlist as $key => $val) {
                $classlist[$key]['is_checkin'] = 0;
                if ($val['is_open_reward'] == 0) {
                    $classlist[$key]['is_checkin'] = checkTodayIsCheckin($userInfo['id'], $val['id']);//0不需要打卡 1已打卡 2未打卡
                }
            }
        }
        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $classlist;
        return json($return);
    }

    /**
     * 获取用户可加入的挑战列表
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function availableLists()
    {
        //
        $auth = trim(input("request.auth"));//用户auth
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '获取失败,未登录', 'devMsg' => '获取失败，参数错误[auth]'];
            return json($return);
        }

        $pagesize = trim(input("pagesize"));//每页条数
        if (!empty($pagesize)) {
            $this->pagesize = $pagesize;
        }
        $currDate = getCurrDate();
        $w = array();
        $w['is_delete'] = 0;//未删除
        $w['stats'] = 0;//已上架
        $w['bm_starttime'] = array('elt',$currDate['date']);//
        $w['bm_endtime'] = array('egt',$currDate['date']);//

        $classlistModel = new \app\admin\model\Classlist();
        $classlist = $classlistModel->lists($w, $this->pagesize);
        $lists = addIsJoinClasslistStatus($classlist['data'], $userInfo['id']);

        $return['err'] = 0;
        $return['msg'] = '获取成功';
        $return['data'] = $lists;
        return json($return);
    }


}
